﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using WorkFlowCore.Authorization;
using WorkFlowCore.Common.EventBus;
using WorkFlowCore.EventData;
using WorkFlowCore.Framework.BackgroundJobs;
using WorkFlowCore.WorkTasks;

namespace WorkFlowCore.Framework.EventHandlers
{
    public class AutoHandleStepsEventHandler : ILocalEventHandler<AutoHandleStepsEventData>,
          ITransientDependency
    {
        private readonly WorkTaskManager workTaskManager;
        private readonly IWorkflowSession session;
        private readonly IBackgroundJobManager backgroundJobManager;

        public AutoHandleStepsEventHandler(WorkTaskManager workTaskManager, IWorkflowSession session, IBackgroundJobManager backgroundJobManager)
        {
            this.workTaskManager = workTaskManager;
            this.session = session;
            this.backgroundJobManager = backgroundJobManager;
        }

        public void Handle(AutoHandleStepsEventData data)
        {
            session.SetUser(data.User);
            if (data == null || data.Steps == null) return;
            foreach (var step in data.Steps)
            {
                workTaskManager.PassApprove(step.Id, data.Comment, string.Empty).Wait();
            }
        }

        public async Task HandleEventAsync(AutoHandleStepsEventData eventData)
        {
            Handle(eventData);
            //await backgroundJobManager.EnqueueAsync(eventData);
        }
    }
}
